﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Security;

using Winsteps.Validator;

public partial class Manage_updatePassword : System.Web.UI.Page
{
    BLL.UserBll ub = new BLL.UserBll();

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            LoadValidator();
        }
    }

    private void LoadValidator()
    {
        WinVal WinValInstance = new WinVal();
        WinValInstance.SetValidator();

        txt_OldPassword.Attributes.Add("onblur", "this.className='inputblur'");
        txt_OldPassword.Attributes.Add("onfocus", "this.className='inputfoucs'");

        txt_NewPassword.Attributes.Add("onblur", "this.className='inputblur'");
        txt_NewPassword.Attributes.Add("onfocus", "this.className='inputfoucs'");

        txt_VerifyPassword.Attributes.Add("onblur", "this.className='inputblur'");
        txt_VerifyPassword.Attributes.Add("onfocus", "this.className='inputfoucs'");
    }
    protected void btn_Submit_Click(object sender, EventArgs e)
    {
        string userId = Page.User.Identity.Name;

        if(ub.GetPassword(userId) == BLL.Cryptography.MD5(txt_OldPassword.Text.ToString()))
        {
            string newp = BLL.Cryptography.MD5(txt_NewPassword.Text.ToString());
            if (ub.UpdatePassword(userId, newp))
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('密码修改成功');window.location.href=window.location.href;", true);
            }
            else
            {
                ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('密码修改失败');window.location.href=window.location.href;", true);
            }
        }
        else
        {
            ClientScript.RegisterClientScriptBlock(this.GetType(), "s", "alert('密码输入错误');window.location.href=window.location.href;", true);
        }
    }
}
